package com.defence.costomapp.activity.manage

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.TextView
import com.defence.costomapp.R
import com.defence.costomapp.base.BaseActivity
import com.defence.costomapp.base.Urls
import com.defence.costomapp.bean.LogDetailJsonBean
import com.defence.costomapp.bean.MachineLogDetailBean
import com.defence.costomapp.utils.SgqUtils
import com.defence.costomapp.utils.httputils.HttpInterface
import com.google.gson.Gson
import com.loopj.android.http.RequestParams
import kotlinx.android.synthetic.main.activity_machine_log.*
import kotlinx.android.synthetic.main.include_title.*
import org.json.JSONObject


class MachineLogActivity : BaseActivity() {

    var stringExtra: String? = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_machine_log)
        //初始化页面
        initView()
        srl.setOnRefreshListener {
            if (iiCount >= 20) {
                iiCount -= 20
                getMachineLogDetail(iiCount)
                logAdapter!!.notifyDataSetChanged()
            } else {
                srl.isRefreshing = false
            }

        }
        //上拉加载
        srl.setOnLoadListener {
            iiCount += 20
            //获取机器日志详情
            getMachineLogDetail(iiCount)
            logAdapter!!.notifyDataSetChanged()
        }
        //获取机器日志详情
        getMachineLogDetail(iiCount)
    }

    private fun initView() {
        back.setText("返回")
        middle_title.setText("机器日志")
        stringExtra = intent.getStringExtra("machineno")
        back.setOnClickListener { finish() }
    }

    internal var iiCount = 0
    private var logAdapter: BaseAdapter? = null
    private var list: MutableList<MachineLogDetailBean.ListBean>? = null

    private fun getMachineLogDetail(iiCount: Int) {
        val params = RequestParams()
        params.add("iCount", iiCount.toString())
        params.add("machine_no", stringExtra)
        httpUtils.doPost(Urls.machinelogdetail(), SgqUtils.MANAGER_TYPE, params, object : HttpInterface() {
            override fun onSuccess(gson: Gson, result: Any, message: String) {
                srl.isRefreshing = false
                srl.setLoading(false)

                val jsonObject = JSONObject(result.toString())
                val logDetailBean = gson.fromJson(jsonObject.toString(), MachineLogDetailBean::class.java)

                if (list == null)
                    list = ArrayList()

                list!!.addAll(logDetailBean.list)

                if (logAdapter == null) {
                    logAdapter = LogAdapter(this@MachineLogActivity, list)
                    list_log.setAdapter(logAdapter)
                } else {
                    logAdapter!!.notifyDataSetChanged()
                }

            }

            override fun onError(context: Context, message: String) {
                super.onError(context, message)
                srl.isRefreshing = false
            }

            override fun onFailure(context: Context) {
                super.onFailure(context)
                srl.isRefreshing = false
            }

            override fun onSpecial(context: Context, sign: String, message: String) {
                super.onSpecial(context, sign, message)
                srl.isRefreshing = false
            }
        })
    }

    private inner class LogAdapter(private val context: Context, private val list: List<MachineLogDetailBean.ListBean>?) : BaseAdapter() {
        private val inflater: LayoutInflater

        init {
            inflater = LayoutInflater.from(context)
        }

        override fun getCount(): Int {
            // TODO Auto-generated method stub
            return if (list != null && list.size > 0) {
                list.size
            } else 0
        }

        override fun getItem(arg0: Int): Any {
            // TODO Auto-generated method stub
            return list!![arg0]
        }

        override fun getItemId(arg0: Int): Long {
            // TODO Auto-generated method stub
            return arg0.toLong()
        }

        override fun getView(position: Int, view: View?, arg2: ViewGroup): View {
            var view = view
            // TODO Auto-generated method stub
            if (view == null) {
                view = inflater.inflate(R.layout.item_machinelogdetail, null)
            }
            var tv_time = view!!.findViewById<TextView>(R.id.tv_time)
            var tv_machineno = view.findViewById<TextView>(R.id.tv_machineno)
            var tv_json = view.findViewById<TextView>(R.id.tv_json)
            val ll_listlog = view.findViewById<LinearLayout>(R.id.ll_listlog)
            val tv_viewkey = view.findViewById<TextView>(R.id.tv_viewkey)
            val tv_istopview = view.findViewById<TextView>(R.id.tv_istopview)
            val tv_isdooropening = view.findViewById<TextView>(R.id.tv_isdooropening)
            val tv_islogin = view.findViewById<TextView>(R.id.tv_islogin)
            val tv_temperature = view.findViewById<TextView>(R.id.tv_temperature)
            val tv_humidty = view.findViewById<TextView>(R.id.tv_humidty)
            val tv_idevicetime = view.findViewById<TextView>(R.id.tv_idevicetime)
            val tv_hasfun = view.findViewById<TextView>(R.id.tv_hasfun)
            val ll_result = view.findViewById<LinearLayout>(R.id.ll_result)
            var ll_machineVersion = view.findViewById<LinearLayout>(R.id.ll_machineVersion)
            var tv_machineVersion = view.findViewById<TextView>(R.id.tv_machineVersion)


            tv_time.text = list!!.get(position).act_time
            tv_machineno.text = intent.getStringExtra("machinename")


            if (list.get(position).act_int == 301) {
                tv_json.visibility = View.GONE
                ll_listlog.visibility = View.VISIBLE

                val jsonObject = list.get(position).json
                val logDetailBean = Gson().fromJson(jsonObject.toString(), LogDetailJsonBean::class.java)

                if (logDetailBean.isTopView.equals("1")) {
                    tv_istopview.text = "是"
                } else {
                    tv_istopview.text = "否"
                }

                if (logDetailBean.viewKey.equals("100")) {
                    tv_viewkey.text = "购买界面"
                } else if (logDetailBean.viewKey.equals("200")) {
                    tv_viewkey.text = "登录界面"
                } else if (logDetailBean.viewKey.equals("210")) {
                    tv_viewkey.text = "补货界面"
                } else {
                    tv_viewkey.text = "维护界面"
                }

                if (logDetailBean.isDoorOpening.equals("1")) {
                    tv_isdooropening.text = "开门"
                } else {
                    tv_isdooropening.text = "关门"
                }

                if (logDetailBean.isLogin.equals("0")) {
                    tv_islogin.text = "未登录"
                } else {
                    tv_islogin.text = "登录中"
                }

                tv_temperature.text = logDetailBean.temperature
                tv_humidty.text = logDetailBean.humidity
                tv_idevicetime.text = logDetailBean.deviceTime
                tv_hasfun.text = logDetailBean.hasRun
                //机器版本
                tv_machineVersion.text = logDetailBean.versionNO


                //        关门不是购买界面
                if (!logDetailBean.isDoorOpening.equals("1") && !logDetailBean.viewKey.equals("100")) {
                    ll_result.setBackgroundColor(Color.YELLOW)
                }
                //             开门是购买界面
                if (logDetailBean.isDoorOpening.equals("1") && logDetailBean.viewKey.equals("100")) {
                    ll_result.setBackgroundColor(Color.YELLOW)
                }

                if (list.get(position).key1 == 2) {
                    ll_machineVersion.visibility = View.VISIBLE
                } else if (list.get(position).key1 == 1) {
                    ll_machineVersion.visibility = View.GONE
                } else {
                    tv_json.visibility = View.VISIBLE
                    ll_listlog.visibility = View.GONE
                    ll_result.setBackgroundColor(Color.parseColor("#f4f4f4"))
                    tv_json.text = list.get(position).json
                }
            } else {
                tv_json.visibility = View.VISIBLE
                ll_listlog.visibility = View.GONE
                ll_result.setBackgroundColor(Color.parseColor("#f4f4f4"))
                tv_json.text = list.get(position).json
            }

            return view!!

        }

    }
}


